﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using TimeoutException = System.TimeoutException;

namespace SCS_350_snmp
{
    class SnmpHelper
    {
        static SnmpHelper()
        {
            Community = "public";
            TimeOut = 3000;
        }

        public static int TimeOut { get; set; }

        public static string Community { get; set; }

        public static List<Variable>[] Walk(string ip, List<ObjectIdentifier> Params, WalkMode walkMode = WalkMode.WithinSubtree/*, List<Variable>[] vars*/)          //Retrieving variables from T-VIPS via SNMP
        {
            var vars = new List<Variable>[Params.Count()];
            var receiver = new IPEndPoint(IPAddress.Parse(ip), 161);
            int i = 0;
            foreach (ObjectIdentifier oi in Params)
            {
                try
                {
                    var var = new List<Variable>();
                    Messenger.Walk(VersionCode.V1, receiver, new OctetString(Community), oi, var, TimeOut, walkMode);
                    vars[i++] = var;
                }
                catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
                {
                    vars[0] = new List<Variable> {new Variable(new ObjectIdentifier(DevicesInt.Errors.NO_CONNECTION_OID))};
                    break;
                }
            }
            return vars;
        }

        public static List<ObjectIdentifier> PrepairForVars(List<string> oids/*, out List<ObjectIdentifier> Params*/)                        //SNMP OIDS for getstatus
        {
            var Params = new List<ObjectIdentifier>(oids.Count);
            Params.AddRange(oids.Select(oid => new ObjectIdentifier(oid)));
            return Params;
            /*Params = new List<ObjectIdentifier>(4)
                    {
                        new ObjectIdentifier("1.3.6.1.4.1.22909.1.1.1.20.1.5"),         //Severity
                        new ObjectIdentifier("1.3.6.1.4.1.22909.1.1.1.20.1.4"),         //Name
                        new ObjectIdentifier("1.3.6.1.4.1.22909.1.1.1.20.1.8"),         //Source
                        new ObjectIdentifier("1.3.6.1.4.1.22909.1.1.1.20.1.9")          //Details
                    };*/
            /*vars = new List<Variable>[4];
            for (int i = 0; i < vars.Length; i++)
            {
                vars[i] = new List<Variable>();
            }*/
        }

        
    }

    class SnmpWorker
    {
        private List<ObjectIdentifier> _statusParams;
        private bool _statusOidsInit;

        private List<ObjectIdentifier> _logParams;
        private bool _logOidsInit;

        public void SetStatusOids(List<string> statusOids)
        {
            if (statusOids.Count < 1)
                return;
            _statusParams = SnmpHelper.PrepairForVars(statusOids);
            _statusOidsInit = true;
        }

        public void SetLogOids(List<string> logOids)
        {
            if (logOids.Count < 1)
                return;
            _logParams = SnmpHelper.PrepairForVars(logOids);
            _logOidsInit = true;
        }

        public List<Variable>[] GetStatusVariables(string ip)          //Return variables for getstatus
        {
            if (!_statusOidsInit)
                throw new Exception("Status OIDs not set.");
            return SnmpHelper.Walk(ip, _statusParams);
        }

        public List<Variable>[] GetLogVariables(string ip)          //Return variables for getstatus
        {
            if (!_logOidsInit)
                throw new Exception("Status OIDs not set.");
            return SnmpHelper.Walk(ip, _logParams);
        }
    }
}
